package com.jorado.etl.taskhandles;

import com.jorado.etl.config.RemoteSettings;
import com.jorado.etl.service.SolrEtlService;
import com.jorado.event.EventClient;
import com.jorado.search.core.config.Solr;
import com.jorado.search.core.config.SolrConfig;
import com.jorado.search.core.exception.NoAssignSolrException;
import com.jorado.search.core.service.ExportService;
import com.jorado.search.core.task.BaseHandler;
import com.jorado.search.solr.client.SolrjClient;
import com.jorado.search.solr.client.SolrjClientPool;
import com.jorado.search.solr.client.SolrjOption;
import org.springframework.context.annotation.Description;
import org.springframework.util.CollectionUtils;

/*
 * Created by len.zhang on 2018/4/17.
 *
 */
@Description("Word2vec语料抽取任务")
public final class Word2vecEtlHandler extends BaseHandler {

    @Override
    protected void exec(Object... args) {

        String trainFile = RemoteSettings.word2vecIncrementTrainFile();
        EventClient.getDefault().asyncSubmitLog("Solr etl beging...");
        ExportService exportService = new SolrEtlService(assignPositionSolr(), trainFile, false);
        exportService.dataExport();
        EventClient.getDefault().asyncSubmitLog("Solr etl over");
    }

    private SolrjOption assignPositionSolr() {

        Solr solr = SolrConfig.getInstance().getSolr();

        if (null == solr || CollectionUtils.isEmpty(solr.getHost())) {
            throw new NoAssignSolrException();
        }

        SolrjClient solrjClient = SolrjClientPool.newClient(solr.getHost(), solr.isCloud());
        return new SolrjOption(solr.getCollection(), solrjClient);
    }
}
